import wx
import os
import pyzipper

class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title, size=(400, 270))

        self.panel = wx.Panel(self)
        self.folder_picker = wx.DirPickerCtrl(self.panel, style=wx.DIRP_USE_TEXTCTRL)
        self.password_text = wx.TextCtrl(self.panel, value='icbc')
        self.start_button = wx.Button(self.panel, label='开始执行')

        self.start_button.Bind(wx.EVT_BUTTON, self.on_start_button_clicked)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(wx.StaticText(self.panel, label='选择文件夹：'), flag=wx.ALL, border=10)
        sizer.Add(self.folder_picker, flag=wx.EXPAND|wx.ALL, border=10)
        sizer.Add(wx.StaticText(self.panel, label='输入密码：'), flag=wx.ALL, border=10)
        sizer.Add(self.password_text, flag=wx.EXPAND|wx.ALL, border=10)
        sizer.Add(self.start_button, flag=wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, border=10)

        self.panel.SetSizer(sizer)
        self.Show()

    def on_start_button_clicked(self, event):
        source_folder = self.folder_picker.GetPath()
        password = self.password_text.GetValue()

        if not os.path.isdir(source_folder):
            wx.MessageBox('请选择有效的文件夹', '错误', wx.OK | wx.ICON_ERROR)
            return

        compress_folders_with_password(source_folder, password)

        wx.MessageBox('压缩完成', '提示', wx.OK | wx.ICON_INFORMATION)

def compress_folders_with_password(source_folder, password):
    for folder_name in os.listdir(source_folder):
        folder_path = os.path.join(source_folder, folder_name)

        if os.path.isdir(folder_path):
            zip_filename = folder_name + '.zip'
            zip_filepath = os.path.join(source_folder, zip_filename)

            with pyzipper.AESZipFile(zip_filepath, 'w', compression=pyzipper.ZIP_DEFLATED, encryption=pyzipper.WZ_AES) as zf:
                zf.setpassword(password.encode())

                for root, dirs, files in os.walk(folder_path):
                    for file in files:
                        file_path = os.path.join(root, file)
                        arcname = os.path.relpath(file_path, folder_path)
                        zf.write(file_path, arcname)


app = wx.App()
frame = MyFrame(None, title='文件夹压缩工具')
app.MainLoop()